﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class control_memberreg : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Wozuji.Logic.Users.Users urs = new Wozuji.Logic.Users.Users();
        int fg = 0;
        //校验昵称输入
        if (urs.NameLogic(userRealName.Text.ToString()))
        {
            fg = fg + 1;
            realNameMsg.Text = "";
        }
        else
        {
            realNameMsg.Text = "昵称输入不合法哦！";
            realNameMsg.ForeColor = System.Drawing.Color.Red;

        }


        //校验用户名输入
        if (urs.NameLogic(userName.Text.ToString()))
        {
            //检测是否存在
            if (urs.NameExist(userName.Text.ToString()))
            {
                realNameMsg.Text = "用户名已经存在！";
                realNameMsg.ForeColor = System.Drawing.Color.Red;
            }
            else
            {

                fg = fg + 1;
                realNameMsg.Text = "";
            }
        }
        else
        {
            userNameMsg.Text = "用户名输入不合法哦！";
            userNameMsg.ForeColor = System.Drawing.Color.Red;

        }
        //校验密码输入
        if (pass1.Text.Length <= 5 || pass1.Text.Length > 50)
        {
            pass1Msg.Text = "密码长度不符合要求！";
            pass1Msg.ForeColor = System.Drawing.Color.Red;

        }
        else if (pass2.Text.Equals(pass1.Text))
        {
            pass1Msg.Text = "";
            pass2Msg.Text = "";
            fg = fg + 1;

        }
        else
        {
            pass2Msg.Text = "两次密码输入不一致！";
            pass2Msg.ForeColor = System.Drawing.Color.Red;
        }



        //校验邮箱输入
        if (email.Text.ToString().Length > 0)
        {
            if (urs.isEmailLogic(email.Text.ToString()))
            {
                if (urs.mailExist(email.Text.ToString()))
                {
                    emailMsg.Text = "E-mail已经存在！";
                    emailMsg.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    fg = fg + 1;
                    emailMsg.Text = "";
                }
            }
            else
            {
                emailMsg.Text = "E-mail不合法哦！";
                emailMsg.ForeColor = System.Drawing.Color.Red;

            }
        }
        //校验验证码

        if (validcode.Text.ToString().ToUpper().Equals(Session["CheckCode"].ToString().ToUpper()))
        {
            fg = fg + 1;
            validMsg.Text = "";
        }
        else
        {
            validMsg.Text = "验证码不正确！";
            validMsg.ForeColor = System.Drawing.Color.Red;
        }
        pass1.Attributes.Add("valus", pass1.Text.ToString());
        pass2.Attributes.Add("valus", pass2.Text.ToString());
        if (fg == 5)
        {
            int memberid=urs.adusers(email.Text.ToString(), userRealName.Text.ToString(), pass1.Text.ToString(), userName.Text.ToString());
            if (memberid>0)
            {

                //检测是否有邀请信息
                Wozuji.Logic.groups gp = new Wozuji.Logic.groups();
                int gpid=gp.getInviteGroupId();
                if (gpid > 0)
                {
                    //用户加入组群
                    gp.joinGroup(gpid, memberid);

                    Wozuji.Common.MessageBox.ShowAndRedirectAjax(Page, "加入组群成功！现在就登录吧！", "/u/userlogin.aspx");
                    gp.removeInviteGroupId();
                }
                else
                {

                    Wozuji.Common.MessageBox.ShowAndRedirectAjax(Page, "注册成功,现在就登录吧！", "/u/userlogin.aspx");
                }
            }
            else
            {
                Wozuji.Common.MessageBox.ShowAjax(Page, "用户信息初始化失败，请稍候再试");
            }
        }
    
    }
}
